/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

$(document).ready(function() {
    $(".deletebtn").click(function() {
        var id = $(this).attr('data-id');
        $.confirm({
            text: "Are you sure you want to delete that document?",
            title: "Confirmation required",
            confirm: function() {
                $.ajax({
                    url: '../admin/delete_doc',
                    data: {ids: [id]},
                    type: 'post',
                    success: function(data) {
                        if (data) {
                            $("#dialog").html(data);
                            $("#dialog").dialog({
                                modal: true,
                                buttons: {
                                    Ok: function() {
                                        window.location.reload();
                                    }
                                }
                            });
                        } else {
                            window.location.reload();
                        }
                    },
                    error: function(data) {
                        $.confirm({
                            text: data.responseText,
                        });
                    }
                });
            },
            cancel: function() {

            }
        });
    });

    $(".publicbtn").click(function() {
        var id = $(this).attr('data-id');
        $.confirm({
            text: "Are you sure you want to public that document?",
            title: "Confirmation required",
            confirm: function() {
                $.ajax({
                    url: '../admin/public_doc',
                    data: {ids: [id]},
                    type: 'post',
                    success: function(data) {
                        if (data) {
                            $("#dialog").html(data);
                            $("#dialog").dialog({
                                modal: true,
                                buttons: {
                                    Ok: function() {
                                        window.location.reload();
                                    }
                                }
                            });
                        } else {
                            window.location.reload();
                        }
                    },
                    error: function(data) {
                        $.confirm({
                            text: data.responseText,
                        });
                    }
                });
            },
            cancel: function() {

            }
        });
    });

    $("#actionbtn").click(function() {
        var ids = [];
        $('.item').each(function() {
            if ($(this).is(":checked")) {
                ids.push($(this).attr('data-id'));
            }
        });
        var action = $("#action").val();
        switch (action) {
            case 'add':
                window.location.href = '../admin/add_doc';
                break;
            case 'edit':
                if (ids.length != 1) {
                    $("#dialog").html("Chỉ được chọn một tài liệu để chỉnh sửa");
                    $("#dialog").dialog({
                        modal: true,
                        buttons: {
                            Ok: function() {
                                $("#dialog").dialog('close');
                            }
                        }
                    });
                } else {
                    window.location.href = '../admin/edit_doc/' + ids[0];
                }
                break;
            case 'trash':
                $.confirm({
                    text: "Are you sure you want to delete these documents?",
                    title: "Confirmation required",
                    confirm: function() {
                        $.ajax({
                            url: '../admin/delete_doc',
                            data: {ids: ids},
                            type: 'post',
                            success: function(data) {
                                if (data) {
                                    $("#dialog").html(data);
                                    $("#dialog").dialog({
                                        modal: true,
                                        buttons: {
                                            Ok: function() {
                                                window.location.reload();
                                            }
                                        }
                                    });
                                } else {
                                    window.location.reload();
                                }
                            },
                            error: function(data) {
                                $.confirm({
                                    text: data.responseText,
                                });
                            }
                        });
                    },
                    cancel: function() {

                    }
                });
                break;
            case 'public':
                $.confirm({
                    text: "Are you sure you want to public these documents?",
                    title: "Confirmation required",
                    confirm: function() {
                        $.ajax({
                            url: '../admin/public_doc',
                            data: {ids: ids},
                            type: 'post',
                            success: function(data) {
                                if (data) {
                                    $("#dialog").html(data);
                                    $("#dialog").dialog({
                                        modal: true,
                                        buttons: {
                                            Ok: function() {
                                                window.location.reload();
                                            }
                                        }
                                    });
                                } else {
                                    window.location.reload();
                                }
                            },
                            error: function(data) {
                                $.confirm({
                                    text: data.responseText,
                                });
                            }
                        });
                    },
                    cancel: function() {

                    }
                });
                break;
            default:
                break;
        }
    })
});
